package com.team.appinfo.interceptor;
/*
* 拦截器
* 防盗链
* */

import com.team.appinfo.entity.BackendUser;
import com.team.appinfo.entity.DevUser;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptror implements HandlerInterceptor {

    /*
     * preHandle,拦截请求时执行的方法(进入控制器之前)
     * 注意：该方法接收返回值，如果返回true,则表示放行可以进入控制器
     *      反之返回false表示不放行，不能进入控制器
     * */

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession();
        DevUser devUser=(DevUser)session.getAttribute("du");
        BackendUser backendUser=(BackendUser)session.getAttribute("bu");
        if ((devUser!=null)||(backendUser!=null)){
            return true;
        }else {
            response.sendRedirect("/goZhuYe");
            return false;
        }

    }

    @Override  //postHandle在业务处理器处理请求执行完成后(控制器执行完后),生成视图之前执行;
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override   //afterCompletion在DispatcherServlet完全处理完请求后被调用,可用于清理资源等 afterCompletion()执行完成后开始渲染页面
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
